import requests
import pandas as pd
import logging
from datetime import datetime
import sys

# 配置日志
logging.basicConfig(
    filename='data_fetch.log',
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)

def fetch_stock_data():
    """获取股票数据并保存到CSV"""
    try:
        logging.info("开始获取股票数据")
        
        # 配置API请求
        url = "https://api.example.com/stocks"
        headers = {
            "User-Agent": "Mozilla/5.0",
            "Accept": "application/json"
        }
        
        # 重试机制(最多3次)
        for attempt in range(3):
            try:
                response = requests.get(
                    url, 
                    headers=headers, 
                    timeout=10
                )
                response.raise_for_status()
                
                # 解析JSON数据
                data = response.json()
                if not data:
                    raise ValueError("API返回空数据")
                
                # 转换为DataFrame
                df = pd.DataFrame(data)
                
                # 验证必要列
                required_columns = [
                    'code', 'name', 'price', 
                    'change_percent', 'volume', 'date'
                ]
                missing = [
                    col for col in required_columns 
                    if col not in df.columns
                ]
                if missing:
                    raise ValueError(f"缺少必要列: {missing}")
                
                # 数据清洗
                df = df.dropna(subset=required_columns)
                df['price'] = pd.to_numeric(df['price'], errors='coerce')
                df['change_percent'] = pd.to_numeric(df['change_percent'], errors='coerce')
                df['volume'] = pd.to_numeric(df['volume'], errors='coerce')
                
                # 保存到CSV
                df.to_csv("../test_data.csv", index=False)
                logging.info(f"成功获取并保存{len(df)}条股票数据")
                return True
                
            except requests.exceptions.RequestException as e:
                logging.warning(f"尝试 {attempt + 1}/3 失败: {str(e)}")
                if attempt == 2:
                    raise
                time.sleep(2)  # 等待2秒后重试
                
    except Exception as e:
        logging.error(f"获取股票数据失败: {str(e)}")
        raise

if __name__ == "__main__":
    try:
        success = fetch_stock_data()
        if not success:
            sys.exit(1)
    except Exception as e:
        logging.critical(f"程序终止: {str(e)}")
        sys.exit(1)